package com.qf.ssm.service.Impl;

import com.qf.ssm.entity.promotion.*;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.FormDataMapper;
import com.qf.ssm.mapper.SaleActivityMapper;
import com.qf.ssm.mapper.SpecialShowMapper;
import com.qf.ssm.service.PromotionService;
import com.qf.ssm.utils.ResponseCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PromotionServiceImpl implements PromotionService {
    @Resource
    private SpecialShowMapper specialShowMapper;
    @Resource
    private SaleActivityMapper saleActivityMapper;

    @Resource
    private FormDataMapper formDataMapper;

    @Override
    public List<SpecialShow> queryBysaName(String saName, String saStartTime, String saEndTime) {
        try {
            List<SpecialShow> specialShow = specialShowMapper.selectBysaName(saName, saStartTime, saEndTime);
            return specialShow;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int addActivitys(SSData specialShow) {
        try {
            int result = specialShowMapper.insertActivitys(specialShow);
            if (result > 0){
                Integer ssId = specialShow.getSsId();
                String saName = specialShow.getSaName();
                System.out.println(saName);
                String saEndTime = specialShow.getSaEndTime();
                System.out.println(saEndTime);


                int values = saleActivityMapper.insertActivitys(specialShow);
                System.out.println(values);
                if (values > 0){
                    return (result+values);
                }else {
                    return 0;
                }
            }else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int editActivity(int ssId, int saState) {
        try {
            int value = saleActivityMapper.updateCombineState(ssId, saState);
            return value;
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    @Override
    public int updateState(Integer id, Integer state) {
        try{
            if (saleActivityMapper.updateActivityState(id,state) > 0){
                return saleActivityMapper.updateActivityState(id,state);
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }

    }

    @Override
    public int editActivity(Integer id, String saName, Integer saOrderNumber, Integer saRealPay) {
        try{
            int result = saleActivityMapper.updateActivitys(id, saName, saOrderNumber, saRealPay);
            if (result > 0){
                return result;
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public FormData queryActivity(Integer id) {
        try{
            FormData formData = saleActivityMapper.selectActivity(id);
            return formData;

        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public int updateSekillState(Integer id, Integer state) {
        try{
            if (saleActivityMapper.updateSekillActState(id,state) > 0){
                return saleActivityMapper.updateSekillActState(id,state);
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public int updateSekillActivity(Integer id, String saName, Integer saOrderNumber, Integer saRealPay) {
        try{
            if (saleActivityMapper.updateSekillActivity(id, saName, saOrderNumber, saRealPay) > 0){
                return saleActivityMapper.updateSekillActivity(id, saName, saOrderNumber, saRealPay);
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public FormData querySekillActivit(Integer id) {
        try{
            return saleActivityMapper.selectSekillActivi(id);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public int insertSekillActivity(BuildSekill buildSekill) {
        try {
            int i = specialShowMapper.insertSekillActivitys(buildSekill);
            if (i > 0){
                Integer ssId = buildSekill.getSsId();
                int j = saleActivityMapper.insertSekillActivity(ssId, buildSekill);
                return i+j;
            }else {
                return 0;
            }
        }catch (Exception e){

            throw new ServiceException(ResponseCode.PROMO_EXCEP);
        }
    }

    @Override
    public List<SpecialShow> querySekillActivity(String saName, String saStartTime, String saEndTime) {
        try{
            return specialShowMapper.selectSekillActivity(saName, saStartTime, saEndTime);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }

    }

    @Override
    public FormData queryGroupActivity(String saName, String saStartTime, String saEndTime) {
        try{
            return formDataMapper.selectGroupInfor(saName, saStartTime, saEndTime);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int editGroupState(Integer ssId,Integer saState) {
        try{
            return saleActivityMapper.updateGroupState(ssId, saState);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int delOneGroup(Integer id) {
        try {
            return saleActivityMapper.delGroupActivitys(id);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }

    }

    @Override
    public int updateGroup(FormData formData) {
        try{
            int i = specialShowMapper.updateGroupAct(formData);
            if (i > 0){
                int j = saleActivityMapper.updateGroupActs(formData);
                return (i+j);
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    @Override
    public FormData queryGroup(int ssId) {
        try {
            FormData formData1 = specialShowMapper.selectAppointActivity(ssId);
            FormData formData2 = saleActivityMapper.queryAppointActivity(ssId);
            if (formData1 != null){
                formData2.setSsName(formData1.getSsName());
                return formData2;
            }else {
                return null;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public SpecialShow queryCombinations(String saName, String saStartTime, String saEndTime) {
        try {
            return specialShowMapper.selectCombinationActivity(saName,saStartTime,saEndTime);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int updateCombineActivity(int ssId, int saState) {
        try {
            int value = saleActivityMapper.updateCombineState(ssId, saState);
            return value;
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }

    }

    @Override
    public int delCombineActivity(int ssId) {
        try{
            return saleActivityMapper.delGroupActivitys(ssId);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    @Override
    public int editCombine(FormData formData) {
        try{
            int i = specialShowMapper.updateCombineActivity(formData);
            if (i > 0){
                int j = saleActivityMapper.updateCombineActivity(formData);
                return (i+j);
            }else {
                return 0;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    @Override
    public FormData queryCombine(int ssId) {
        try{
            FormData formData = specialShowMapper.selectCombineActivity(ssId);
            FormData formData1 = saleActivityMapper.selectCombieActivity(ssId);
            if (formData != null){
                formData1.setSsName(formData.getSsName());
                formData1.setSsGroupOrder(formData.getSsGroupOrder());
                formData1.setSsGroupNumber(formData1.getSsGroupNumber());
                formData1.setSsGroupPay(formData.getSsGroupPay());
                return formData1;
            }else {
                return null;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int editAppointSta(int ssId, int saState) {
        try {
            int value = saleActivityMapper.updateCombineState(ssId, saState);
            return value;
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    @Override
    public SpecialShow queryAppoints(String saName, String saStartTime, String saEndTime) {
        try {
            return specialShowMapper.selectCombinationActivity(saName, saStartTime, saEndTime);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }

    @Override
    public int delAppoint(int ssId) {
        try {
            return saleActivityMapper.delGroupActivitys(ssId);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    @Override
    public int editAppoint(FormData formData) {
        try{
            int i = specialShowMapper.updateAppointActivity(formData);
            int j = saleActivityMapper.updateAppointActivity(formData);
            return (i+j);
        }catch (Exception e){
            throw new ServiceException(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    @Override
    public FormData queryAppointActivity(int ssId) {
        try {
            FormData formData = specialShowMapper.selectAppointActivity(ssId);
            FormData formData1 = saleActivityMapper.queryAppointActivity(ssId);
            if (formData != null){
                formData1.setSsName(formData.getSsName());
                return formData1;
            }else {
                return null;
            }
        }catch (Exception e){
            throw new ServiceException(ResponseCode.BRAND_SELECT);
        }
    }
}
